package com.imooc.ecommerce.entity;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.imooc.ecommerce.constant.BrandCategory;
import com.imooc.ecommerce.constant.GoodsCategory;
import com.imooc.ecommerce.constant.GoodsStatus;
import com.imooc.ecommerce.goods.GoodsInfo;
import com.imooc.ecommerce.goods.SimpleGoodsInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

/**
 * 商品表实体类定义
 * @author zhangshao
 * @since 2023/11/29 21:50
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("t_ecommerce_goods")
public class EcommerceGoods {
    /**
     * 自增主键
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 商品类型
     */
    @TableField("goods_category")
    private GoodsCategory goodsCategory;

    /**
     * 品牌分类
     */
    @TableField("brand_category")
    private BrandCategory brandCategory;

    /**
     * 商品名称
     */
    @TableField("goods_name")
    private String goodsName;

    @TableField("goods_pic")
    private String goodsPic;

    /**
     * 商品描述信息
     */
    @TableField("goods_description")
    private String goodsDescription;

    /**
     * 商品状态
     */
    @TableField("goods_status")
    private GoodsStatus goodsStatus;

    /**
     * 商品价格 单位: 分、厘
     */
    @TableField("price")
    private Integer price;

    /**
     * 总供应量
     */
    @TableField("supply")
    private Long supply;

    /**
     * 库存
     */
    @TableField("inventory")
    private Long inventory;

    /**
     * 商品属性,json字符串存储
     */
    @TableField("goods_property")
    private String goodsProperty;

    /**
     * 创建时间
     */
    @TableField(value = "create_time",fill= FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
    private Date createTime;

    /**
     * 更新时间
     */
    @TableField(value = "update_time",fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
    private Date updateTime;

    /**
     * 将GoodsInfo 转换为实体对象
     * @param goodsInfo
     * @return
     */
    public static EcommerceGoods to(GoodsInfo goodsInfo){

        EcommerceGoods ecommerceGoods = new EcommerceGoods();
        ecommerceGoods.setGoodsCategory(GoodsCategory.of(goodsInfo.getGoodsCategory()));
        ecommerceGoods.setBrandCategory(BrandCategory.of(goodsInfo.getBrandCategory()));
        ecommerceGoods.setGoodsName(goodsInfo.getGoodsName());
        ecommerceGoods.setGoodsPic(goodsInfo.getGoodsPic());
        ecommerceGoods.setGoodsDescription(goodsInfo.getGoodsDescription());
        // 可以增加一个审核的状态，审核之后商品上线
        ecommerceGoods.setGoodsStatus(GoodsStatus.ONLINE);
        ecommerceGoods.setPrice(goodsInfo.getPrice());
        ecommerceGoods.setSupply(goodsInfo.getSupply());
        //创建时刻为supply
        ecommerceGoods.setInventory(goodsInfo.getSupply());
        ecommerceGoods.setGoodsProperty(
                JSON.toJSONString(goodsInfo.getGoodsProperty())
        );

        return ecommerceGoods;
    }

    /**
     * 将实体对象转为goodsInfo对象
     */
    public GoodsInfo toGoodsInfo(){
        GoodsInfo goodsInfo = new GoodsInfo();

        goodsInfo.setId(this.id);
        goodsInfo.setGoodsCategory(this.goodsCategory.getCode());
        goodsInfo.setBrandCategory(this.brandCategory.getCode());
        goodsInfo.setGoodsName(this.goodsName);
        goodsInfo.setGoodsPic(this.goodsPic);
        goodsInfo.setGoodsDescription(this.goodsDescription);
        goodsInfo.setGoodsStatus(this.goodsStatus.getStatus());
        goodsInfo.setPrice(this.price);
        goodsInfo.setGoodsProperty(
                JSON.parseObject(this.goodsProperty, GoodsInfo.GoodsProperty.class)
        );
        goodsInfo.setSupply(this.supply);
        goodsInfo.setInventory(this.inventory);
        goodsInfo.setCreateTime(this.createTime);
        goodsInfo.setUpdateTime(this.updateTime);

        return goodsInfo;
    }


    /**
     * 将实体对象转成 SimpleGoodsInfo 对
     */
    public SimpleGoodsInfo toSimple(){
        SimpleGoodsInfo goodsInfo = new SimpleGoodsInfo();

        goodsInfo.setId(this.id);
        goodsInfo.setGoodsName(this.goodsName);
        goodsInfo.setGoodsPic(this.goodsPic);
        goodsInfo.setPrice(this.price);

        return goodsInfo;
    }


}
